%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
clear all; close all; clc;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Modify main_str to match path on your machine
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
main_str = 'C:\Users\Marco\Dropbox\Housing_Brainstorming\Information_disclosure';
% main_str = 'L:\agargano\Dropbox\Housing_Brainstorming\Information_disclosure';

save_str = [main_str,'\Replication_code_RFS\Model'];

% setup grid for valuation errors' mu and sigma
biasM_grid        = (-0.050:0.025:0.050); % mu
biasS_grid        = ( 0.000:0.020:0.100); % sigma
[m_grid1,m_grid2] = ndgrid(biasM_grid,biasS_grid);
bias_grid_mat     = [m_grid1(:),m_grid2(:)];
n_bias            = size(bias_grid_mat,1);

for bb = 1:n_bias

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %% Seller Reservation and Bidders Values

    seller_reservation     = 650000;
    bidders_mean           = 650000;
    % bidders_std            = 65000;
    bidders_std            = 32500;
    bidders_logstd         = sqrt(log((bidders_std^2)/exp(2*log(bidders_mean))+1));
    bidders_logmean        = log(bidders_mean)-((bidders_logstd^2)/2);

    n_sim                  = 1000000;  % number of random draws for bids

    start_bid_scale        = (0.75:0.010:1.25); 
    start_bid              = start_bid_scale*seller_reservation; % vector of entry bids (1+alpha)*seller reservation
    n_start                = size(start_bid,2);

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    mu_b                   = bias_grid_mat(bb,1); % select value of mean valuation error
    std_b                  = bias_grid_mat(bb,2); % select value of std valuation error

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    n_bidders_all_vec      = (1:1:15); % number of bidders vector
    i_all                  = size(n_bidders_all_vec,2);

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %% Simulations

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Allocate memory for outcomes
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    Psales_mat       = nan(i_all,n_start);
    Ebidders_mat     = nan(i_all,n_start);
    EbidderW_mat     = nan(i_all,n_start);
    EbidderW_obj_mat = nan(i_all,n_start);
    SbidderW_obj_mat = nan(i_all,n_start);    
    Esales_mat       = nan(i_all,n_start);
    Erev_mat         = nan(i_all,n_start);

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    poolobj         = parpool('local');
    % % tic;

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    parfor i = 1:i_all % parallel loop over number of bidders
    % for i = 1:i_all

        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        start_bid_curr   = start_bid; % vector of starting bids
        bids_values_mat  = lognrnd(bidders_logmean,bidders_logstd,n_bidders_all_vec(i),n_sim); % draw ``true private values''
        bias_curr_mat    = exp(normrnd(mu_b-(0.5*(std_b^2)),std_b,n_bidders_all_vec(i),n_sim)); % valuation errors: exp(e), with e normally distributed
        if i > 1
            [bids_values_bias_mat,sel_unb_mat] = sort(bids_values_mat.*bias_curr_mat,'descend'); % observed (noisy) private values, sorted
        elseif i == 1
            bids_values_bias_mat = bids_values_mat.*bias_curr_mat; % observed (noisy) private values, one bidder
            sel_unb_mat          = ones(1,n_sim);
        end

        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
        for k = 1:n_start % loop over starting bids

            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
            % Allocate memory
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
            sales_succ_vec       = zeros(n_sim,1);
            bidder_value_vec     = zeros(n_sim,1);
            bidder_value_obj_vec = zeros(n_sim,1);            
            sales_price_vec      = nan(n_sim,1);
            nbidders_vec         = zeros(n_sim,1);            
 
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%            
            for n = 1:n_sim % loop over random draws
    
                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                bids_values_bias_vec  = bids_values_bias_mat(:,n); % get private values draw      
                is_act                = bids_values_bias_vec>start_bid_curr(k); % select private values above starting bid

                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                
                nbidders_vec(n,1)     = sum(is_act); % number of bidders entering the auction
                
                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
                if nbidders_vec(n,1) == 1 % there is only one bidder entering the auction
                     sales_price_vec(n,1)      = start_bid_curr(k); % sales price is equal to entry bid
                     bidder_value_vec(n,1)     = bids_values_bias_vec(1)-start_bid_curr(k); % perceived surplus
                     bidder_value_obj_vec(n,1) = bids_values_mat(sel_unb_mat(1,n),n)-start_bid_curr(k); % actual surplus  
                     sales_succ_vec(n,1)       = 1; % house sells   
                elseif nbidders_vec(n,1) > 1 % multiple bidders in the auction   
                     sales_price_vec(n,1)      = bids_values_bias_vec(2); % sale at second highest perceived bid
                     bidder_value_vec(n,1)     = bids_values_bias_vec(1)-sales_price_vec(n,1); % perceived surplus 
                     bidder_value_obj_vec(n,1) = bids_values_mat(sel_unb_mat(1,n),n)-sales_price_vec(n,1); % actual surplus                      
                     sales_succ_vec(n,1)       = 1; % house sells       
                end    
            
            end
     
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            Psales_mat(i,k)        = sum(sales_succ_vec)/n_sim; % sales probability
            Ebidders_mat(i,k)      = nanmean(nbidders_vec)/n_bidders_all_vec(i); % expected share of bidders entering the auction
            EbidderW_mat(i,k)      = nanmean(bidder_value_vec)/n_bidders_all_vec(i); % expected surplus (perceived)
            EbidderW_obj_mat(i,k)  = nanmean(bidder_value_obj_vec)/n_bidders_all_vec(i); % expected surplus (objective)
            Esales_mat(i,k)        = nanmean(sales_price_vec)-seller_reservation; % expected sales price - minus seller reservation value
            Erev_mat(i,k)          = Psales_mat(i,k)*Esales_mat(i,k); % expected seller revenue       
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%        
        
        end
    
    end

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % toc;
    delete(poolobj);

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %% Save

    save([save_str,'\Output\Last\BiasInd\equilibrium_step1_last_bias_mu',num2str(bias_grid_mat(bb,1)*100),'_std',num2str(bias_grid_mat(bb,2)*100),'.mat']);
    
    bias_grid_mat(bb,:)

    clearvars -except bias_grid_mat n_bias bb main_str save_str

end

